package handlers;

import java.util.Date;

import racegame.extensions.RaceGameRoomExtension;

import com.smartfoxserver.v2.annotations.Instantiation;
import com.smartfoxserver.v2.annotations.Instantiation.InstantiationMode;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

@Instantiation(InstantiationMode.SINGLE_INSTANCE)
public class RacersUpdate extends BaseClientRequestHandler {

	RaceGameRoomExtension gameExtension;
	String TIMESTAMP_RECEIVED	= "R";
	
	@Override
	public void handleClientRequest(User user, ISFSObject params) {
		
		// Get a reference to the parent extension
		gameExtension = (RaceGameRoomExtension)getParentExtension();
		
		// Store the time the game is created
		if (gameExtension.startTime == 0)
		{
			Date now = new Date();
			gameExtension.startTime = now.getTime();
		}
		
		// Add the offset from race start that this message was received and add to the incoming object
		Date now = new Date();
		long currentTime = now.getTime();
		long received_time = currentTime - gameExtension.startTime;
		params.putLong(TIMESTAMP_RECEIVED, received_time);
		
		// Add the user's incoming update object to the object sent to all racers.
		//Set key as playerId to string and value is SFSObject containing x and y position of player
		gameExtension.playersInfo.putSFSObject(String.valueOf(user.getPlayerId()), params);
		trace("PlayerId: " + String.valueOf(user.getPlayerId()) + " " + params);
	
	}

}
